{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:tf.op_scope(values, name, default_name) is deprecated, use tf.name_scope(name, default_name, values)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "using Yang's load pretrained weights version\n",
      "('conv1', 'weights', (11, 11, 3, 96))\n",
      "('weight = ', 0.0)\n",
      "('conv1', 'biases', (96,))\n",
      "(20, 55, 55, 96)\n",
      "('conv2', 'weights', (5, 5, 48, 256))\n",
      "('weight = ', 0.0)\n",
      "('conv2', 'biases', (256,))\n",
      "(20, 27, 27, 256)\n",
      "('conv3', 'weights', (3, 3, 256, 384))\n",
      "('weight = ', 0.0)\n",
      "('conv3', 'biases', (384,))\n",
      "(20, 13, 13, 384)\n",
      "('conv4', 'weights', (3, 3, 192, 384))\n",
      "('weight = ', 0.0)\n",
      "('conv4', 'biases', (384,))\n",
      "(20, 13, 13, 384)\n",
      "('conv5', 'weights', (3, 3, 192, 256))\n",
      "('weight = ', 0.0)\n",
      "('conv5', 'biases', (256,))\n",
      "(20, 13, 13, 256)\n",
      "('fc6', 'weights', (9216, 4096))\n",
      "('weight = ', 0.0)\n",
      "('fc6', 'biases', (4096,))\n",
      "(20, 1, 1, 4096)\n",
      "('fc7', 'weights', (4096, 4096))\n",
      "('weight = ', 0.0)\n",
      "('fc7', 'biases', (4096,))\n",
      "(20, 1, 1, 4096)\n",
      "('fc8', 'weights', (4096, 1000))\n",
      "('weight = ', 0.0)\n",
      "('fc8', 'biases', (1000,))\n",
      "(20, 1, 1, 1000)\n",
      "---------------------------------------- TrainStage1_discrete_tcnn1\n",
      "----------------------------------------\n",
      "All tensors that will be summarized:\n",
      "Tensor(\"CNN_FC/TrainStage1_discrete_tcnn1/temporal_conv/Relu:0\", shape=(1, 20, 1, 64), dtype=float32)\n",
      "Tensor(\"CNN_FC/TrainStage1_discrete_tcnn1/softmax_linear_car_discrete/BiasAdd:0\", shape=(20, 6), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "import wrapper\n",
    "import tensorflow as tf\n",
    "from tensorflow.core.example import example_pb2\n",
    "from cStringIO import StringIO\n",
    "from PIL import Image\n",
    "from matplotlib.pyplot import imshow\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "\n",
    "a = wrapper.Wrapper(\"discrete_tcnn1\", \n",
    "            \"/data/yang/code/BDD_Driving_Model/data/discrete_tcnn1/model.ckpt-126001.bestmodel\",\n",
    "            20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "example = example_pb2.Example()\n",
    "in_file = \"/data/yang_cache/tfrecord_release/tfrecords/validation/ca737ec3-53cd36e2.tfrecords\"\n",
    "\n",
    "count = 0\n",
    "for example_serialized in tf.python_io.tf_record_iterator(in_file):\n",
    "    example.ParseFromString(example_serialized)\n",
    "    feature_map = example.features.feature\n",
    "    encoded = feature_map['image/encoded'].bytes_list.value\n",
    "    print count\n",
    "    count += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "file_jpgdata = StringIO(encoded[0])\n",
    "dt = Image.open(file_jpgdata)\n",
    "imshow(np.asarray(dt))\n",
    "print a.observe_a_frame(np.asarray(dt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(len(encoded)):\n",
    "    if i % 5 == 0:\n",
    "        file_jpgdata = StringIO(encoded[0])\n",
    "        dt = Image.open(file_jpgdata)\n",
    "        arr = np.asarray(dt)\n",
    "        out = a.observe_a_frame(arr)\n",
    "        print out\n",
    "        print i/5"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}